/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.rrx360.common.model.BaseEntity;
import com.rrx360.common.model.SoftDeleteAble;

public class BaseDao<T> extends CommonDao {

  protected Class<?> entityClass;

  public BaseDao() {
    Type superType = getClass().getGenericSuperclass();
    if (superType instanceof Class) {
      superType = ((Class) superType).getGenericSuperclass();
    }
    if (superType instanceof ParameterizedType) {
      entityClass = (Class<T>) ((ParameterizedType) superType).getActualTypeArguments()[0];
    } else {
      entityClass = null;
    }

  }



  public Class<?> getEntityClass() {
    return entityClass;
  }



  public void setEntityClass(Class<?> entityClass) {
    this.entityClass = entityClass;
  }



  public T create() {
    return (T) super.create(entityClass);
  }

  public T load(Long id) {
    return (T) load(entityClass, id);
  }

  public T get(Long id) {
    return (T) get(entityClass, id);
  }

  public void save(T entity) {
    saveEntity(entity);
  }

  public void delete(T entity) {
    deleteEntity(entity);
  }


  public void update(T entity) {
    updateEntity(entity);
  }

  public void saveOrUpdate(T entity) {
    saveOrUpdateEntity(entity);
  }

  public List<T> findAll() {
    return (List<T>) findAll(entityClass);
  }

}
